package com.atguigu.gmall.product.init;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lhh
 * @ClassName InitRunner
 * @date 2022 2022/12/6
 * 项目启动初始化位图
 */
@Slf4j
@Component
public class InitRunner implements CommandLineRunner {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Resource
    SkuInfoService skuInfoService;

    public void initBitMap() {
        log.info("始化化 skuid-bitmap。。。。。");
        List<SkuInfo> ids = skuInfoService.lambdaQuery().select(SkuInfo::getId).list();
        ids.stream().forEach(item->{
            redisTemplate.opsForValue().setBit(RedisConst.SKUID_BITMAP,item.getId(),true);
        });
        log.info("初始化完成 skuid-bitmap完成");
    }
//    SpringApplication application;
//    public InitRunner(SpringApplication application,String[] args){
//        log.info("监听器对象创建 application：{}，args：{}",application,args);
//        this.application=application;
//    }

    @Override
    public void run(String... args) throws Exception {
        log.info("InitRunner。。。启动run,skuInfoService:{}",skuInfoService);
        initBitMap();
    }
}
